﻿namespace Probel.Ldoc.ViewModels.ToolBoxes
{
    /*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
    */
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;

    public class CreateFileViewModel : BaseViewModel
    {
        #region Fields

        private readonly ICommand cancelCommand;
        private readonly ICommand createRecordCommand;
        private readonly RecordEditorViewModel ParentViewModel;

        private string recordName = DateTime.Today.ToShortDateString();
        private RecordDrawerCardDto selectedFolder;

        #endregion Fields

        #region Constructors

        public CreateFileViewModel(RecordEditorViewModel parentViewModel
            , IErrorService errorService
            , bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.ParentViewModel = parentViewModel;
            this.Folders = new ObservableCollection<RecordDrawerCardDto>();
            this.createRecordCommand = new RelayCommand(this.CreateRecord, this.CanCreateRecord);
            this.cancelCommand = new RelayCommand(this.Cancel);
        }

        #endregion Constructors

        #region Properties

        public ICommand CancelCommand
        {
            get { return this.cancelCommand; }
        }

        public ICommand CreateRecordCommand
        {
            get { return this.createRecordCommand; }
        }

        public ObservableCollection<RecordDrawerCardDto> Folders
        {
            get;
            private set;
        }

        public string RecordName
        {
            get { return this.recordName; }
            set
            {
                this.recordName = value;
                this.OnPropertyChanged(() => RecordName);
            }
        }

        public RecordDrawerCardDto SelectedFolder
        {
            get { return this.selectedFolder; }
            set
            {
                this.selectedFolder = value;
                this.OnPropertyChanged(() => SelectedFolder);
            }
        }

        #endregion Properties

        #region Methods

        public void Refresh()
        {
            this.Async(() => this.ParentViewModel.GetRecordFolders()
                      , t => { this.Folders.Refill(t); });
        }

        private void Cancel()
        {
            this.ParentViewModel.UiMessage = UiMessageDto.Info(Messages.Msg_CancelRecordCreation);
        }

        private bool CanCreateRecord()
        {
            return !string.IsNullOrWhiteSpace(this.recordName) && this.SelectedFolder != null;
        }

        private void CreateRecord()
        {
            var record = new RecordCardDto() { Name = this.RecordName };
            this.ParentViewModel.CreateRecord(record, this.SelectedFolder);
        }

        #endregion Methods
    }
}